package controller;

import java.util.Date;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import server.ClientState;
import server.IProtocolHandler;
import xml.Message;

/**
 * This controller hanldes the use case where the administrator requests to delete
 * one or more events from the Decision Lines system.
 * 
 * @author Billy
 *
 */
public class DeleteEventController implements IProtocolHandler {

	@Override
	public Message process(ClientState state, Message request) {
		
		// Get handle to the system
		entity.System system = entity.System.getInstance();
		
		Node removeRequest = request.contents.getFirstChild();
		NamedNodeMap removeMap = removeRequest.getAttributes();
		
		// Retrieve the key, return with an error message if it is invalid
		String key = removeMap.getNamedItem("key").getNodeValue();
		if (!system.validateAdminKey(key)) {
			String response = Message.responseHeader(request.contents.getAttributes().getNamedItem("id")
					.getNodeValue(), "Invalid key");
			response += "<removeResponse numberAffected='0' /></response>";
			return new Message(response);
		}
		
		// Initialize the number of events that have been deleted
		int deleted = 0;
		
		// Retrieve the value of the ID attribute. If present, delete the event with that
		// ID and update the count accordingly.
		Node idNode = removeMap.getNamedItem("id");
		if (idNode != null) {
			String id = idNode.getNodeValue();
			if (system.deleteEventById(id)) {
				deleted ++;
			}
		}
		
		// Retrieve the values of the completed and daysOld attributes. If present,
		// delete all events that match these parameters and update the count accordingly.
		Node completedNode = removeMap.getNamedItem("completed");
		Node daysOldNode = removeMap.getNamedItem("daysOld");
		if (completedNode != null && daysOldNode != null) {
			boolean completed = Boolean.parseBoolean(completedNode.getNodeValue());
			String daysOld = daysOldNode.getNodeValue();
			
			// Determine the cutoff Date for deletion
			long days = Long.parseLong(daysOld);
			Date cutoff = new Date((new Date()).getTime() - days * 24 * 60 * 60 * 1000);
			deleted += system.deleteEventByCriteria(completed, cutoff);
		}
		
		// Send the response
		String response = Message.responseHeader(request.contents.getAttributes().getNamedItem("id")
				.getNodeValue());
		response += "<removeResponse numberAffected='" + deleted + "' /></response>";
		return new Message(response);
	}

}

/* Relevant schema
<!-- RemoveRequest           -->
<!-- _______________________ -->

<xs:element name='removeRequest'> 
  <xs:complexType>
    <xs:attribute name='key'  type='xs:string' use='required'/>
   
    <!-- remove a single event? use this one -->
    <xs:attribute name='id'   type='xs:string' use='optional'/>

    <!-- remove completed/uncompleted events more then X days old. Add these two -->
    <xs:attribute name='completed' type='xs:boolean' use='optional'/>
    <xs:attribute name='daysOld'   type='xs:int' use='optional'/>

  </xs:complexType>
</xs:element>

<xs:element name='removeResponse'>
  <xs:complexType>
    <xs:attribute name='numberAffected'  type='xs:int' use='required'/>
  </xs:complexType>
</xs:element>
*/